import pymongo
import pymongo.read_preferences

mongo_url = "mongodb://rwuser:Y3fF7#12FF!!D9980!fds@172.25.117.219:8635,172.25.117.55:8635,172.25.117.35:8635/bigdata_v2?authSource=admin&replicaSet=replica&w=majority&readPreference=secondary"
mongo_client = pymongo.MongoClient(mongo_url, minPoolSize=5)
testdb = mongo_client.test

test_col = testdb.test_col


with (
    mongo_client.start_session(causal_consistency=True) as session,
    session.start_transaction(read_preference=pymongo.ReadPreference.PRIMARY),
):
    test_col.insert_one({"key": "k1", "value": 1}, session=session)
    cnt = test_col.count_documents({}, session=session)
    assert cnt == 1

    test_col.delete_one({"key": "k1"}, session=session)
    cnt = test_col.count_documents({}, session=session)
    assert cnt == 0
